layui.define(function (exports) {

    var a = layui.jquery,
        admin_map = a("#admin_map").val(),
        {createEditor, createToolbar} = window.wangEditor,
        T = {
            init: function () {
                a('.wang-editor').each(function () {
                    const filed = a(this).attr('data-filed');
                    const value = a(this).attr('data-value');
                    const editorConfig = {
                        placeholder: '在此处填写内容...',
                        filed: filed,
                        MENU_CONF: {},
                        onCreated(editor) {
                            const html = editor.getHtml()
                            a(".editor—" + filed).val(html === "<p><br></p>" ? "" : html)
                        },
                        onChange(editor) {
                            const html = editor.getHtml()
                            a(".editor—" + filed).val(html === "<p><br></p>" ? "" : html)
                        }
                    }
                    editorConfig.MENU_CONF['uploadImage'] = {
                        server: '/' + admin_map + '/gallery/upload.html',
                        fieldName: 'file',
                        meta: {
                            type: 1,
                            editor: true
                        },
                        // customBrowseAndUpload(insertFn) {
                        //     var h = 630;
                        //     if (a(window).height() < h) h = (a(window).height() - 20);
                        //     parent.layer.open({
                        //         type: 2,
                        //         area: ['1000px', h + 'px'],
                        //         btn: ['使用选中图片', '取消选择'],
                        //         fix: false, //不固定
                        //         maxmin: true,
                        //         shadeClose: true,
                        //         shade: 0.4,
                        //         anim: 2,
                        //         title: '选择图片',
                        //         content: '/' + admin_map + '/gallery/open.html?c=10&n=选择图片&editor=1&v=&t=1',
                        //         yes: function (index, layero) {
                        //             var body = parent.layer.getChildFrame('body', index);
                        //             var a = body.find('#selectedUrl').val();
                        //             var n = a.split(',');
                        //             for (var i = 0; i < n.length; i++) {
                        //                 insertFn(n[i])
                        //             }
                        //             parent.layer.close(index);
                        //         }
                        //     });
                        // }
                    }
                    editorConfig.MENU_CONF['uploadVideo'] = {
                        // 上传视频的配置
                        server: a("#uploadEditorVideo").val(),
                        maxFileSize: 50 * 1024 * 1024, // 5M
                        maxNumberOfFiles: 3,
                        allowedFileTypes: ['video/*'],
                    }

                    const editor = createEditor({
                        selector: '#editor-container-' + filed,
                        html: value,
                        config: editorConfig,
                        mode: 'default', // or 'simple'
                    })
                    const toolbar = createToolbar({
                        editor,
                        selector: '#toolbar-container-' + filed,
                        // config: toolbarConfig,
                        mode: 'default', // or 'simple'
                    })
                })
            }
        }
    exports('wangEditor', T);
});